<template>
  <div>
      <el-card  class="co-middle">
          <el-form :inline="true" :model="pageing" class="demo-form-inline">
             <el-form-item label="车型编号:" prop="id">
    <el-input v-model="pageing.id" placeholder="请输入车型编号" ></el-input>
  </el-form-item >
  <el-form-item label="应载重量:" label-width="200px">
    <el-select v-model="pageing.allowableLoad" placeholder="请选择应载重量">
      <el-option v-for="item in Freightmanagement.allowableLoad " :key="item.id" :label="item.value" :value="item.id">
      </el-option>
    </el-select>
  </el-form-item >
   <el-form-item label="应载体积:"  label-width="220px">
    <el-select v-model="formInline.allowableVolume" placeholder="请选择应载体积">
      <el-option v-for="item in Freightmanagement.allowableVolume " :key="item.id" :label="item.value" :value="item.id"></el-option>
    </el-select>
  </el-form-item>
        </el-form>
        <!--  -->
        <el-form :inline="true" :model="pageing" class="demo-form-inline">
  <el-form-item label="车辆类型:" prop="name">
    <el-input v-model="pageing.name" placeholder="请输入车辆类型"></el-input>
  </el-form-item>
   <el-form-item class="anniu">
    <el-button type="danger" @click="serch">搜索</el-button>
    <el-button @click="onSubmit">重置</el-button>
  </el-form-item>
       </el-form>
      </el-card>
      <!-- 下半部分 -->
      <el-card>
        <el-row>
          <el-button  type="danger" @click="addType">新增车型</el-button>
        </el-row>
        <!-- 表格部分 -->
          <el-table :data="tableData"
      style="width: 100%">
      <el-table-column  label="序号"  type="index"> </el-table-column>
      <el-table-column prop="id" label="车型编号" width="180px"> </el-table-column>
      <el-table-column prop="name" label="车型类型"  width="180px"> </el-table-column>
      <el-table-column prop="num" label="车型数量" > </el-table-column>
      <el-table-column prop="allowableLoad" label="应载重量(吨)" > </el-table-column>
      <el-table-column prop="allowableVolume" label="应载体积(立方米)" > </el-table-column>
      <el-table-column prop="measureLong" label="长(米)" > </el-table-column>
      <el-table-column prop="measureWidth" label="宽(米)" > </el-table-column>
      <el-table-column prop="measureHigh" label="高(米)" > </el-table-column>
      <el-table-column  label="操作" >
        <!-- 插槽 -->
        <template #default="{row}">
         <el-button  type="text" @click="addVehicleType(row.id)">编辑</el-button>
         <el-divider direction="vertical" ></el-divider>
          <el-button style="color:red"  type="text" @click="del(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
        <!-- 分页 -->
       <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageing.page"
      :page-sizes="[1, 2, 3, 5]"
      :page-size="pageing.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
      </el-card>
      <!-- 新增弹层 -->
  <el-dialog
  title="新增车型"
  :visible="showDialog"
  width="40%"
  @close="closeForm">
 <el-form ref="addFormRef" :model="addForm" label-width="80px" :rules="rules">
  <el-form-item label="车辆类型" prop="name">
    <el-input v-model="addForm.name" placeholder="请输入车辆类型"></el-input>
  </el-form-item>
   <el-form-item label="应载重量" prop="allowableLoad">
    <el-input v-model="addForm.allowableLoad" placeholder="请输入车型载重"></el-input>
  </el-form-item>
   <el-form-item label="应载体积" prop="allowableVolume">
    <el-input v-model="addForm.allowableVolume" placeholder="请输入车型体积"></el-input>
  </el-form-item>
   <el-form-item label="长" prop="measureLong">
     <el-input  type="text" placeholder="请输入长"  autocomplete="off" v-model="addForm.measureLong">
      <template slot="suffix" >米</template>
     </el-input>
  </el-form-item>
   <el-form-item label="宽" prop="measureWidth">
    <el-input type="text"  placeholder="请输入宽"  autocomplete="off" v-model="addForm.measureWidth">
         <template slot="suffix">米</template>
    </el-input>
  </el-form-item>
   <el-form-item label="高" prop="measureHigh">
    <el-input type="text"  placeholder="请输入高"  autocomplete="off" v-model="addForm.measureHigh">
         <template slot="suffix">米</template>
    </el-input>
  </el-form-item>
 </el-form>
   <template #footer>
    <el-row type="flex" justify="center">
       <el-button type="danger" @click="addUser">确认</el-button>
    <el-button  @click="showDialog = false" >取消</el-button>
    </el-row>
   </template>
</el-dialog>
  </div>
</template>

<script>
import { addDetails, delVehicleApi, getDetails, getPagingApi, getVehicleDetailsApi } from '@/api/chelmanagement'
// import { getDetails } from '@/api/chelmanagement'
import Freightmanagement from '@/api/constant/freightmanagement'
export default {
  name: 'chel-management',
  data () {
    return {
      form: {},
      Freightmanagement,
      showDialog: false,
      formInline: {
        allowableLoad: '',
        allowableVolume: ''
      },
      tableData: [],
      idValue: null, // 存id
      rules: {
        name: [
          {
            required: true, message: '车辆名称不能为空', trigger: 'blur'
          }
        ],
        allowableLoad: [
          {
            required: true, message: '只可输入1-100之间的整数', trigger: 'blur'
          }
        ],
        allowableVolume: [
          {
            required: true, message: '只可输入1-100之间的整数', trigger: 'blur'
          }
        ]

      },
      // 添加的数据
      addForm: {},
      pageing: {
        page: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  methods: {

    // 新增
    async addType () {
      this.showDialog = true
      // const res = await addDetails(this.tableData)
      // console.log(res)
    },
    // 添加和编辑弹层的确认按钮
    async addUser () {
      if (this.addForm.id) {
        // id存在就是编辑
        await addDetails(this.addForm)
        this.$message.success('编辑成功')
        this.geiUserList()
      } else {
        // 添加逻辑
        await this.$refs.addFormRef.validate()
        await addDetails(this.addForm)
        this.$message.success('添加成功')
        this.geiUserList()
      }
    },

    closeForm () {
      // this.$refs.addFormRef.validateField()
    },
    // 重置
    onSubmit () {
      this.pageing = {
        page: 1,
        pageSize: 10
      }
      this.getVehicleList()
    },
    // 分页
    // async geiUserList () {
    //   const res = await getPagingApi(this.pageing)
    //   console.log(res)
    // },
    // 获取车辆详情
    async  getVehicleList () {
      const res = await getVehicleDetailsApi()
      // console.log(res)
      this.tableData = res.data
      // this.getVehicleList()
      // console.log(this.tableData)
      // 分页
      // this.idValue = res.id
      // console.log(this.idValue)
      // const re = await getPagingApi(this.pageing)
      // console.log(re)
    },
    //  编辑
    async addVehicleType (id) {
      // console.log(id)
      const { data: res } = await getDetails(id)
      console.log(res)
      this.addForm = res
      this.showDialog = true
    },
    // 删除
    async  del (id) {
      // console.log(id)
      // 给删除提示
      await this.$confirm('是否确认删除')
      // 调接口删除
      await delVehicleApi(id)
      // 删除成功给提示
      // this.$message.success('删除成功')
      this.$message.success('删除成功')
      // 重新获取最新列表
      this.getVehicleList()
    },
    handleSizeChange (newSize) {
      console.log(newSize)
      // 新的页码值
      this.pageing.pageSize = newSize
      // 重新获取数据页面
      this.getVehicleList()
      // this.geiUserList()
    },
    handleCurrentChange (newPage) {
      console.log(newPage)
      this.pageing.page = newPage
      // 重新获取数据页面
      this.getVehicleList()
      // this.geiUserList()
    },
    // 分页数据
    async   geiUserList () {
      // console.log(this.pageing)
      const { data: res } = await getPagingApi(this.pageing)
      console.log(res)
      this.pageing.pageSize = res.pageSize
      this.pageing.page = res.page
      this.total = +res.counts
    },
    // 搜索
    async  serch () {
      const res = await getPagingApi(this.pageing)
      console.log(res)
      this.tableData = res.data.items
    }

  },
  created () {
    // 分页的函数
    this.geiUserList()
    this.getVehicleList()
  }
}
</script>

<style lang="scss" scoped>
 .co-middle {
        margin-bottom: 20px;
}
.anniu{
  margin-left: 50px;
}
i {
  margin: 0 5px;
}
</style>
